home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d21 / apishare.arc / SHAREB.C < prev    next >
Text File  |  1988-10-22  |  3KB  |  129 lines

  1. /****************************************************************
  2. *
  3. *  Name:          SHAREB
  4. *
  5. *  Function:      share memory/data with another process
  6. *
  7. *  Shows how to:  1. read from and write to shared memory.
  8. *                 2. receive from another process the address of shared data.
  9. *                 3. control access to shared data via mailbox semaphore.
  10. *
  11. ****************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include "dvapi.h"
  15.  
  16. /* minimum API version required */
  17. #define required 0x201
  18.  
  19. /* API version number */
  20. int version;
  21.  
  22. /* default object handle */
  23. ulong win;
  24.  
  25. /* application handle of other process */
  26. ulong apphana;
  27.  
  28. /* mail-related variables */
  29. int status;
  30. char *malptr;
  31. int mallng;
  32.  
  33. /* type declarations related to shared data */
  34. typedef char *DATATYPE;
  35. typedef DATATYPE *DATAPTR;
  36.  
  37. /* constant value to be assigned to shared memory */
  38. DATATYPE shrconst = "     BBBBB";
  39.  
  40. /* pointer to shared data */
  41. DATAPTR shrptr;
  42.  
  43. /* pointer to pointer to shared data */
  44. DATAPTR *shrptrptr;
  45.  
  46. /* mailbox semaphore controlling access to shared memory */
  47. ulong sema;
  48.  
  49. /* global name of mailbox */
  50. char *name = "Shared Memory Semaphore";
  51.  
  52.  
  53. /**********************************************************************
  54. *  main  -  check for DESQview present and enable required extensions.
  55. ***********************************************************************/
  56.  
  57. main () {
  58.   /* initialize C interfaces and get API version number */
  59.   version = api_init();
  60.  
  61.   /* if DESQview is not running or version is too low, display a message */
  62.   if (version < required) {
  63.     printf ("This program requires DESQview version %d.02%d or later.\n",
  64.              required/256,required%256);
  65.     }
  66.  
  67.   else {
  68.     /* tell DESQview what extensions to enable and start application */
  69.     api_level (required);
  70.     program_body();
  71.  
  72.     /* disable C interfaces and return from program */
  73.     api_exit();
  74.     }
  75.  
  76.   }
  77.  
  78.  
  79. /********************************************************************
  80. /*  program_body
  81. /********************************************************************/
  82.  
  83. program_body () {
  84.  
  85.   /* get object handle */
  86.   win = win_me();
  87.  
  88.   /* copy initial data into shared memory */
  89.   *shrptr = shrconst;
  90.  
  91.   /* find named mailbox semaphore */
  92.   sema = mal_find (name,sizeof (name));
  93.  
  94.   /* read pointer to mailed data */
  95.   status = mal_read (mal_me(),shrptrptr,mallng);
  96.  
  97.   /* assign pointer to shared data by dereferencing pointer to pointer */
  98.   shrptr = *shrptrptr;
  99.  
  100.   /* get the application task handle of other process */
  101.   apphana = mal_addr (mal_me());
  102.  
  103.   /* begin critical region */
  104.   api_beginc();
  105.  
  106.   /* loop till handle of other process is no longer valid */
  107.   while (api_isobj (apphana)) {
  108.  
  109.     /* lock semaphore */
  110.     mal_lock (sema);
  111.  
  112.     /* read & display current contents & address of shared data */
  113.     win_printf (win,"%s at %Fp\n",*shrptr,*shrptr);
  114.  
  115.     /* modify contents of shared data */
  116.     strcpy (*shrptr,shrconst);
  117.  
  118.     /* unlock semaphore */
  119.     mal_unlock (sema);
  120.  
  121.     /* end critical region */
  122.     api_endc();
  123.  
  124.     /* begin critical region */
  125.     api_beginc();
  126.     }
  127.  
  128.   }
  129.